<template>
    <div class="view-container deeditview task-edit-view">
<!--
        &lt;!&ndash;        头部，项目名称&ndash;&gt;
        <app-taskpage-projectheader :viewparams="viewparams"
                                    :context="JSON.parse(JSON.stringify(context))"
                                    :enable_even_update="true"
                                    @getActionData="(id) =>{getActionData(id)}">
        </app-taskpage-projectheader>

        &lt;!&ndash;        项目名称等描述&ndash;&gt;
        <app-taskdetail :viewparams="viewparams"
                        :context="JSON.parse(JSON.stringify(context))"
                        :enable_even_update="true"
                        ref="appTaskdetail"
                        @getActionData="(id) =>{getActionData(id)}">
        </app-taskdetail>

        &lt;!&ndash;            action记录，包括评论&ndash;&gt;
        <app-task-actiondata :viewparams="viewparams"
                             :context="JSON.parse(JSON.stringify(context))"
                             :enable_even_update="true"
                             ref="appTaskActiondata">
        </app-task-actiondata>

        &lt;!&ndash;        发表评论处&ndash;&gt;
        <app-task-issuecomment :viewparams="viewparams"
                               :context="JSON.parse(JSON.stringify(context))"
                               :enable_even_update="true"
                               ref="AppTaskIssuecomment"
                               @getActionData="(id) =>{getActionData(id)}">
        </app-task-issuecomment>-->

    </div>
</template>

<script lang='tsx'>
    import {Component} from 'vue-property-decorator';
    import TaskEditViewBase from './task-edit-view-base.vue';

    import view_form from '@widgets/task/main-form/main-form.vue';
    import CodeListService from "@codelist/codelist-service";
    import ActionService from "@service/action/action-service";
    import {Environment} from "@/environments/environment";

    @Component({
        components: {
            view_form,
        },
        beforeRouteEnter: (to: any, from: any, next: any) => {
            next((vm: any) => {
                if (!Object.is(vm.navModel, "route")) {
                    vm.initNavDataWithTab(vm.viewCacheData);
                }
                vm.$store.commit('addCurPageViewtag', {fullPath: to.fullPath, viewtag: vm.viewtag});
            });
        },
    })
    export default class TaskEditView extends TaskEditViewBase {

        public created() {
            let taskId = this.context.task;
            if (taskId) {
                // 查询默认成员
                this.getUserFetchDefault(taskId);
            } else {
                taskId = this.viewparams.taskId;
                if (taskId) {
                    // 查询默认成员
                    this.getUserFetchDefault(taskId);
                } else {
                    const url: any = '/404';
                    this.$router.push({path: url});
                }
            }
        }

        /**
         * 查询默认成员(全部成员)
         */
        public getUserFetchDefault(taskId:any) {
            return new Promise((resolve: any, reject: any) => {
                let result: Promise<any>;
                let data = {srfparentdename: 'Project', srfparentkey: taskId};
                Object.assign(data, this.page);
                result = this.appEntityService.UserFetchDefault({}, data, true);
                result.then((response) => {
                    if (response && response.status === 200) {
                        const data = response.data;
                        resolve(this.doItems(data));
                    } else {
                        resolve([]);
                    }
                }).catch(response => {
                    // console.log(fetch);
                    this.$Notice.error({title: '错误', desc: response.data.detail});
                    reject(response);
                });
            });
        }

        /**
         * 处理数据
         *
         * @public
         * @param {any[]} items
         * @returns {any[]}
         * @memberof UserRealName
         */
        public doItems(items: any[]): any[] {
            let allDefaultUser: any = [];
            items.forEach((item: any) => {
                let itemdata: any = {};
                Object.assign(itemdata, {id: item.account});
                Object.assign(itemdata, {value: item.account});
                Object.assign(itemdata, {text: item.realname});
                Object.assign(itemdata, {label: item.realname});

                allDefaultUser.push(itemdata);
            });

            // 任务描述的富文本编辑器
            let appTaskdetail: any = this.$refs.appTaskdetail;
            let taskDetailRichtext: any = appTaskdetail.$refs.taskDetailRichtext;
            taskDetailRichtext.items = allDefaultUser;

            // action记录的富文本编辑器
            let appTaskActiondata: any = this.$refs.appTaskActiondata;
            for (let i = 0; i < appTaskActiondata.actionItems.length; i++) {
                let actionRichTextEditor: any = appTaskActiondata.$refs['actionRichTextEditor' + i];
                actionRichTextEditor.items = allDefaultUser;
            }

            // 评论的富文本编辑器
            let appTaskIssuecomment: any = this.$refs.AppTaskIssuecomment;
            let commentRichTextEditor = appTaskIssuecomment.$refs.commentRichTextEditor;
            commentRichTextEditor.items = allDefaultUser;

            return allDefaultUser;
        }


        /**
         *分页对象
         *
         *@memberof ProjectListView
         */
        private page: any = {
            size: 1000,
            page: 0,
        };

        /**
         * 获取任务相关action记录
         */
        public getActionData(id:any){
            let appTaskActiondata: any = this.$refs.appTaskActiondata;
            if(appTaskActiondata && appTaskActiondata.searchActionByTask){
                appTaskActiondata.searchActionByTask(id);
            }
        }

    }
</script>